﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FY2013.Model.UserModels;
using FY2013.Model;
using System.Web;

namespace FY2013.Business
{
    public class UserHelper
    {
        public static void Register(RegisterAccountModel model,string ipAddress,string domainUrl)
        {
            if (model == null)
            {
                throw new Exception("注册信息错误，请重试！");
            }
            if (string.IsNullOrWhiteSpace(ipAddress))
            {
                throw new Exception("获取客户端IP失败，服务器禁止无IP客户端注册用户！");
            }

            if (string.IsNullOrWhiteSpace(domainUrl))
            {
                throw new Exception("获取本站网址错误！");
            }

            using(Entities entity=new Entities() )
            {
                Users user = new Users();
                user.Account = model.Account;
                user.Email = model.Email;
                user.FirstLanguage = model.FirstLanguage;
                user.Integral = 0;
                user.Introduction = model.Introduction;
                user.MessageCount = 0;
                user.CreateIP = ipAddress;
                user.CreateTime = DateTime.Now;
                user.LargePortrait = string.Format("{0}/largeAvatar.jpg", domainUrl);
                user.LastLoginIP = ipAddress;
                user.LastLoginTime = DateTime.Now;
                user.SmallPortrait = string.Format("{0}/smallAvatar.jpg", domainUrl);
                user.RoleType = (int)RoleType.普通会员;
                user.Status = (int)UserStatus.Actived;
                user.Password = SecurityHelper.Md5Encrypt(model.Password);
                entity.Users.AddObject(user);
                if (entity.SaveChanges() <1)
                {
                    throw new Exception("用户注册失败，请重试！");
                }

            }
        }
    }
}
